<?php
// 定义角色列表
$characters = [
    [
        "name" => "曹操",
        "hp" => 4,
        "skill" => "奸雄：可以立即获得对你造成伤害的牌。"
    ],
    [
        "name" => "刘备",
        "hp" => 4,
        "skill" => "仁德：出牌阶段，可以将任意数量的手牌交给其他角色，若你于此阶段内给出的牌张数达到两张或更多时，你回复 1 点体力。"
    ],
    [
        "name" => "孙权",
        "hp" => 4,
        "skill" => "制衡：出牌阶段，你可以弃置任意数量的牌，然后摸取等量的牌。每阶段限一次。"
    ]
];

// 定义卡牌列表
$cards = [
    [
        "name" => "杀",
        "type" => "attack",
        "description" => "出牌阶段，对攻击范围内的一名其他角色使用，效果是对该角色造成 1 点伤害。"
    ],
    [
        "name" => "闪",
        "type" => "defense",
        "description" => "当你受到【杀】的攻击时，可以使用此牌进行响应，抵消【杀】的效果。"
    ],
    [
        "name" => "桃",
        "type" => "heal",
        "description" => "出牌阶段，你可以使用此牌回复 1 点体力；当你处于濒死状态时，也可以使用此牌自救。"
    ]
];

// 处理角色选择
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["character"])) {
    $selectedCharacterIndex = $_POST["character"];
    $selectedCharacter = $characters[$selectedCharacterIndex];
    // 初始手牌，随机抽取 3 张
    $handCards = [];
    for ($i = 0; $i < 3; $i++) {
        $randomCardIndex = array_rand($cards);
        $handCards[] = $cards[$randomCardIndex];
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三国杀网页游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f4f4f4;
        }

        h1 {
            color: #333;
        }

        form {
            margin-top: 20px;
        }

        select {
            padding: 8px;
            font-size: 16px;
        }

        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 5px;
        }

        button:hover {
            background-color: #0056b3;
        }

        #character-info {
            margin-top: 20px;
            padding: 20px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            display: inline-block;
        }

        #hand-cards {
            margin-top: 20px;
        }

        #hand-cards ul {
            list-style-type: none;
            padding: 0;
        }

        #hand-cards li {
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <h1>三国杀网页游戏</h1>
    <?php if (!isset($selectedCharacter)): ?>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <label for="character">选择你的角色:</label>
            <select id="character" name="character">
                <?php
                foreach ($characters as $index => $character) {
                    echo '<option value="' . $index. '">' . $character["name"] . '（体力：' . $character["hp"] . '） - ' . $character["skill"] . '</option>';
                }
                ?>
            </select>
            <button type="submit">确认选择</button>
        </form>
    <?php else: ?>
        <div id="character-info">
            <h2>你选择的角色是：<?php echo $selectedCharacter["name"];?></h2>
            <p>体力：<?php echo $selectedCharacter["hp"];?></p>
            <p>技能：<?php echo $selectedCharacter["skill"];?></p>
        </div>
        <div id="hand-cards">
            <h2>你的手牌：</h2>
            <ul>
                <?php
                foreach ($handCards as $card) {
                    echo '<li>' . $card["name"] . ' - ' . $card["description"] . '</li>';
                }
                ?>
            </ul>
        </div>
    <?php endif;?>
</body>

</html>